package com.utils;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.takeshequ.Constants;

public class HttpClientUtils {

	
	/********** system functions*************/
	static {
		System.setProperty("http.proxyHost", "10.0.2.2");
		System.setProperty("http.proxyPort", "8087");
	}

	public interface HttpResponseHandler {
		void handler(String response);
	}

	
	
	
	public static void post(String path, Map<String, String> params,
			final HttpResponseHandler handler) {
		AsyncHttpClient client = new AsyncHttpClient();
		client.setTimeout(60000);
		params.put("appSource", "A");
		params.put("appVersion", "1");
		params.put("regSource", "mobile");
		params.put("memberId", Constants.memberId);
		params.put("gpsLatitude", Constants.latitude);
		params.put("gpsLongitude", Constants.longitude);
		client.post(Constants.domainAndPort + "/" + path, new RequestParams(
				params), new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String response) {
				handler.handler(response);
			}
		});
	}

	public static void get(String path, Map<String, String> params,
			final HttpResponseHandler handler) {
		AsyncHttpClient client = new AsyncHttpClient();
		params.put("appSource", "A");
		params.put("appVersion", "1");
		params.put("regSource", "mobile");
		params.put("memberId", Constants.memberId);
		params.put("gpsLatitude", Constants.latitude);
		params.put("gpsLongitude", Constants.longitude);
		client.get(Constants.domainAndPort + "/" + path, new RequestParams(
				params), new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String response) {
				handler.handler(response);
			}
		});
	}


	public static void post(Context context, String content, String path,
			Map<String, String> params, final HttpResponseHandler handler) {
		AsyncHttpClient client = new AsyncHttpClient();
		client.setTimeout(5000);
		params.put("appSource", "A");
		params.put("appVersion", "1");
		params.put("regSource", "mobile");
		params.put("memberId", Constants.memberId);
		params.put("gpsLatitude", Constants.latitude);
		params.put("gpsLongitude", Constants.longitude);
		client.post(Constants.domainAndPort + "/" + path, new RequestParams(
				params),
				new BasicAsyncHttpResponseHandler(context, "", content) {
					@Override
					public void onSuccessCustomer(String response) {
						handler.handler(response);

					}
				});
	}

	public static void get(Context context, String content, String path,
			Map<String, String> params, final HttpResponseHandler handler) {
		AsyncHttpClient client = new AsyncHttpClient();
		client.setTimeout(5000);
		params.put("appSource", "A");
		params.put("appVersion", "1");
		params.put("regSource", "mobile");
		params.put("memberId", Constants.memberId);
		params.put("gpsLatitude", Constants.latitude);
		params.put("gpsLongitude", Constants.longitude);
		client.get(Constants.domainAndPort + "/" + path, new RequestParams(
				params),
				new BasicAsyncHttpResponseHandler(context, "", content) {
					@Override
					public void onSuccessCustomer(String response) {
						handler.handler(response);

					}
				});
	}

	/*************customization functions************/
	public static void getPersonalInfo(final HttpResponseHandler handler) {
		AsyncHttpClient client = new AsyncHttpClient();
		Map<String,String> params=new HashMap<String,String>();
		params.put("appSource", "A");
		params.put("appVersion", "1");
		params.put("regSource", "mobile");
		params.put("memberId", Constants.selectMemberId);
		params.put("gpsLatitude", Constants.latitude);
		params.put("gpsLongitude", Constants.longitude);
		client.get(Constants.domainAndPort + "/memberinfo", new RequestParams(
				params), new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String response) {
				handler.handler(response);
			}
		});
	}
}
